package com.minimalist.common.utils;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SM4;

/**
 * SM4加解密工具类
 *
 * @author 11987
 */
public class Sm4Util{

    /**
     * 加密
     */
    public static String encrypt(String data){
        byte[] keyBytes = hexToArray();
        SM4 sm4 = SmUtil.sm4(keyBytes);
        return sm4.encryptHex(data);
    }

    /**
     * 解密
     */
    public static String decrypt(String encryptedData){
        byte[] keyBytes = hexToArray();
        SM4 sm4 = SmUtil.sm4(keyBytes);
        return sm4.decryptStr(encryptedData, CharsetUtil.CHARSET_UTF_8);
    }

    private static byte[] hexToArray(){
        String hexKey = "1234567890abcdef1234567890abcdef";
        int len = hexKey.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2){
            data[i / 2] = (byte) ((Character.digit(hexKey.charAt(i), 16) << 4)
                    + Character.digit(hexKey.charAt(i + 1), 16));
        }
        return data;
    }


    public static void main(String[] args){
        String url = "jdbc:dm://hcsdyyds.com:30236/MINIMALIST?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true&clobAsString=1";
        String username = "SYSDBA";
        String password = "222222";
        // 加密用户名和密码
        String encryptedUrl = Sm4Util.encrypt(url);
        String encryptedUsername = Sm4Util.encrypt(username);
        String encryptedPassword = Sm4Util.encrypt(password);
        System.out.println("Encrypted URL: " + encryptedUrl);
        System.out.println("Encrypted Username: " + encryptedUsername);
        System.out.println("Encrypted Password: " + encryptedPassword);
        String data = Sm4Util.decrypt("42a8a3b3489f852093794bff33c3cf4300124e8334c688da5a21c8438a70ccce53bd204b53d06736d8f81613d1d2354d2bdf0ceb02a203bd947a5d4a56de1e48a085d3c68c5490f0b5efc41f68d0434ac77fed1ce35fbb38269f4d788843c19b53bd204b53d06736d8f81613d1d2354d44fc74ad08c68b63d35d469ba1c598b0f90b068119692132438a21d3f464aea284823392d8565396cab62be53f9afcdfe120fcd950c37f2358edfed3ed004292160e2a14e4eedb5cee480e3c7eb40d94f9818e003201d9d434fd97a7fe64cdbb8b257125880d8a28cd4e764c882b7311d00367494c356c8afe9e543491853006f17d0550181398a995120e5b1c88f6ac4c73fe69ea2b6eadc90296d4b84c2e6c482a7d55de2787bd942262244f6622c7b97f8648e9f9103bc0b838e5287070fcb1c4397a99546fe02c57d8b1bf484f2928830d2bdeeacf889410f787688b28e54124e768932059d3d1a0719f0bcc254948917e52cbf553b514643c06e90e6aafdc511ecc3ea3c53757f4365005559437a97da3cd7c87a5f274536e7e4b5d2075bb1f4238886cff551b8f2b77f111f9a2e8833a5ed1fa27f0d719d6735125d27ef572c1ee887c0550adf9a3f2350ed026863a70c95a539eb934b7d8738373d522cc66e0665c7942c6d61a615da8cf2b9b4622bf85a30d636ff588aa247c1fa47dcf0391a8c1a0370fb90c7ee360ef06979aab4d53a5908bce5549369aa993303c04a002ed80ae08a3d9303810b5fbbb4f8245f8ac2b2b3ca00e241c5db59cfd286ae05c5d312f27e5ed41af5e0d7f59ca6814ec97f80dcf9e0f31256516cfcd7a29d2f53700cce4a7aa07aade06da211365430578571b935eb05bfd265290fb7aced3a51588c9a8f99169b34171da66c52dfc8fb0ce41adef0dd1e352227d5f724d957e5fb7f7114cab33c8c4621c7d4dfc9e677fec07c0b4f3051941f8b2b2b9ef4c91ea15d64fe14320333cbdc0f76837914e17a9a1f58dae04e4662ddd7361c43f313db19f516dee6dc1cc5060c0fb0741135163dc410e6aa000c23b13bbfa5d508336dd1bc139d69fb1ca832d33590b166f53688c46d60f1a358f9d33cfb78a08395031c1fe5d26a9547af9f9d2933ae5b8afebb25ab64893ff2b983947e8330cda645a53d50e3a8aa1d9eaeab3fcaba5ad8de1b092aea6325837cea82d69d9e2f120d742d8519c8865cfa54be383c6267c0be5a8a45e8991960307d8c65e623abe96f67bbe785ad73c21a1e5eb8ea37e7b5e9a33dd6c13d9325228dc5196487a448c028511a9a13b9f210b42d3eb0df59d34b38f87044f17b0592bd30f3c9b300b5df32076f26bb2b5c4d69ce86b7a80920877dd60b7a64f4476c73e605190f1423d85bf4936f4e6a60e03b61f22871f94adb36c0e0aa289451e6ac645b40ba4aa98c02c169409c4c740257d818309122891df371b54e5eb3d82396bb956af55b84e40d06571cfbc87c168d9e75af34a240a39a23b461097dd20564d22b38a8292e81af43cf70559ef7a196b07d489725166ccf80297c3e2893cec80912dd439ad685c16150157676486c7bf886a9442d91fdb50b10308fe300e2c90073b8a2a545d5481bf3dd32841395a6e5ac58a449914232d071b371382276fa8338861e5e18e94dab711fa77021ec6855c29bbe2fe0b608ef3096fc57c9c439877246524a82242f1de94f80c8fc343de72480bd3c971f6a097dfc7316ff9fdd3cc45e41e8b68d0d4378b12c6c7f39b4fd0d82176085ed7f75ded3d620ea831e620507c253f4dfef6a2bda802149ac3a97c4f80ece6546dbe32d5405243ed11c5044ba6ff3c2a1f4419f070f7f806d44a04b47ce27b3fd3193d1d535b3c925e1c1afa88ff769160c163c4f4233eeb664ec1fe801f9f96b2dc6863fe8e264c8ec2fdcb51ac3bb15fcf341ad9ca51bf98933ca8771671951ee46cf820cfb0e951e13e9fed72ba17a4a3dd1a1126418525ed753b70f6078f587ccdf9d90e64004497eabf9a12a18d3e80116a1c631762a39c5774d56a0b883b6b24c985c7e437960a6151bbf6209259fb63001819edb6bd135f9b7a0cadd28e2fda4a8f7394f9c83a591fffd1142f73409082b037ae12255663591271932262aec548e7654fdd84b3357512c9ac4c664175a9dd61e08e5d4bd8161767586b9854c236add8c3af7331fd0205d23ddec80eb2857db8b02523527bcb3cc10d56138fff81ddd106e36574793ef97d8dde9104084f56202e2ea547f99dc191ef96761cc3c04a160d9bc1ac7e7c1a3c34ee439c2d4d86bfef44de205a1c259e60497e5dc9e4a9947dec9a9ac6e2310d2d08ed7c68584826ab2d034d5d64");
        System.out.println("decrypt: " + data);
    }
}
